from core.settings import UPLOAD_PATH

def deployResource(cursor,deployId,type,url):
    if not url :
        return
    url = url[len('/'+UPLOAD_PATH):]
    if getResource(cursor,deployId) :
        changeResource(cursor,deployId,url)
    else:
        addResource(cursor,deployId,type,url)
    
def addResource(cursor,deployId,type,url):
    sql = """ insert into dic_res(`deploy_id`,`type`,`url`,`status`,`version`) values(%s,%s,%s,%s,%s) """
    cursor.execute(sql,(deployId,type,url,0,1))
    
def getResource(cursor,deployId):
    sql = """ select url from dic_res where deploy_id=%s"""
    cursor.execute(sql,(deployId,))
    urltuple = cursor.fetchone()
    if urltuple :
        return '/' + UPLOAD_PATH + urltuple[0]
    return None

def changeResource(cursor,deployId,url):
    sql = """ update dic_res set url = %s where deploy_id=%s"""
    cursor.execute(sql,(url,deployId,))